#ifndef SERIAL_DRIVER__SERIAL_DRIVER_NODE_HPP_
#define SERIAL_DRIVER__SERIAL_DRIVER_NODE_HPP_

#include <rclcpp/rclcpp.hpp>
#include <serial_driver/serial_driver.hpp>
#include <geometry_msgs/msg/pose_stamped.hpp>
#include <tf2/LinearMath/Quaternion.h>
#include <tf2/LinearMath/Matrix3x3.h>

class SerialDriverNode : public rclcpp::Node
{
public:
  SerialDriverNode();
  ~SerialDriverNode();

private:
  // 串口对象
  std::unique_ptr<drivers::serial_driver::SerialDriver> serial_driver_;
  std::unique_ptr<drivers::serial_driver::SerialPortConfig> port_config_;
  std::shared_ptr<drivers::common::IoContext> ctx_;
  
  // 订阅器
  rclcpp::Subscription<geometry_msgs::msg::PoseStamped>::SharedPtr pose_sub_;
  
  // 定时器
  rclcpp::TimerBase::SharedPtr timer_;
  
  // 参数
  std::string port_name_;
  int baud_rate_;
  
  // 回调函数
  void poseCallback(const geometry_msgs::msg::PoseStamped::SharedPtr msg);
  void timerCallback();
  
  // 串口管理函数
  bool initializeSerialPort();
  void closeSerialPort();
  
  // CRC8校验计算
  uint8_t calculateCRC8(const uint8_t *data, size_t length);
  
  // 数据发送函数
  void sendSerialData(const geometry_msgs::msg::PoseStamped::SharedPtr msg, bool is_detected);
};

#endif  // SERIAL_DRIVER__SERIAL_DRIVER_NODE_HPP_ 